#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_exci2.info <<'%EOF%'
   geoopt_exci2
   ------------
   Molecule:         LiH
   Wave Function:    MCSCF (CAS)
   Test Purpose:     Optimization of the first excited state of A1
                     symmetry using the .OPTIMIZE module (1st order),
                     and with a vibrational analysis at the final point
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_exci2.mol <<'%EOF%'
INTGRL
Geometry optimization of excited state, vibrational analysis at
optimized geometry
Atomtypes=2 Generators=2 X Y
Blocks=3 1 1    1 Charge=3.0 Atoms=1
Li    0.0 0.0 0.0
F  10    5
1359.4466  .000844  .00000000 .00000000 .00000000 .00000000
204.02647  .006486  .00000000 .00000000 .00000000 .00000000
46.549541  .032477  .00000000 .00000000 .00000000 .00000000
13.232594  .117420  .00000000 .00000000 .00000000 .00000000
4.286148  .294580  .00000000 .00000000 .00000000 .00000000
1.495542 .00000000  .449515  .00000000 .00000000 .00000000
0.542238 .00000000  .255823  .00000000 .00000000 .00000000
0.073968 .00000000 .00000000  1.       .00000000 .00000000
0.028095 .00000000 .00000000 .00000000  1.       .00000000
0.010671 .00000000 .00000000 .00000000 .00000000  1.
F   6    3
4.17  .005112  .00000000 .00000000
1.17256  .020907  .00000000 .00000000
0.32927  .091672  .00000000 .00000000
0.09271  .449260  .00000000 .00000000
0.02607 .00000000  1.       .00000000
0.007331 .00000000 .00000000  1.
F   4    2
0.32927  .159757  .00000000
0.09271 1.475484  .00000000
0.02607 .00000000  .370578
0.007331 .00000000  .017847
Charge=1.0 Atoms=1 Blocks=2 1 1
H     0.0 0.0 3.015
F   6    3
33.685014 .006068 0.00000 0.00000
 5.094788 .045316 0.00000 0.00000
 1.158786 .202846 0.00000 0.00000
 0.325840 .503709 0.00000 0.00000
 0.102741 0.00000 1.00000 0.00000
 0.032400 0.00000 0.00000 1.00000
F   4    2
1.1588 .188440 .000000
 0.3258 .882420 .000000
 0.1027 0.00000 .117800
 0.0324 0.00000 .004200
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_exci2.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
**WAVE FUNCTION
.HF
.MP2
.MCSCF
*SCF INPUT
.DOUBLY OCCUPIED
 2 0 0 0
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 1 0 0 0
.ELECTRONS
 2
.CAS SPACE
 3 1 1 0
*OPTIMIZATION
.SIMULTANEOUS ROOTS
 2 2
.STATE
 2
.OPTIMAL ORBITAL TRIAL VECTORS
**PROPERTIES
.VIBANA
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_exci2.check
cat >>geoopt_exci2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Optimization setup
CRIT1=`$GREP "Default 1st order method will be used\: * BFGS update\." $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in redundant internal coordinates" $log | wc -l`
CRIT3=`$GREP "Model Hessian will be used as initial Hessian\." $log | wc -l`
CRIT4=`$GREP "The model Hessian parameters of Roland Lindh will be used\." $log | wc -l`
CRIT5=`$GREP "Trust region method will be used to control step \(default\)\." $log | wc -l`
CRIT6=`$GREP "1st Order Geometry Optimization" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[1]=6
ERROR[1]="GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"

# Basis set
CRIT1=`$GREP "Li * 1 * 3\.0000 * 48 * 24 * \[10s6p4d\|5s3p2d\]" $log | wc -l`
CRIT2=`$GREP "H * 1 * 1\.0000 * 18 * 9 * \[6s4p\|3s2p\]" $log | wc -l`
CRIT3=`$GREP "total\: * 2 * 4\.0000 * 66 * 33" $log | wc -l`
CRIT4=`$GREP "Spherical harmonic basis used\." $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[2]=4
ERROR[2]="BASIS SET NOT READ CORRECTLY"

# Geometry
CRIT1=`$GREP "Total number of coordinates\: * 6" $log | wc -l`
CRIT2=`$GREP "bond distance\: * H * Li * 1\.59546." $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2`
CTRL[3]=3
ERROR[3]="GEOMETRY NOT READ CORRECTLY"

# Symmetry
CRIT1=`$GREP "Number of coordinates in each symmetry\: * 2 * 2 * 2 * 0" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry\: * 17 * 7 * 7 * 2" $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2`
CTRL[4]=2
ERROR[4]="SYMMETRY NOT CORRECT"

# Energies
CRIT1=`$GREP "Hartree\-Fock total energy *\: * \-7\.98595678.." $log | wc -l`
CRIT2=`$GREP "\= MP2 second order energy *\: * \-8\.0235540..." $log | wc -l`
CRIT3=`$GREP "Final MCSCF energy\: * \-7\.8884600874.." $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[5]=3
ERROR[5]="ENERGIES NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "Li * z * 0*\.0284" $log | wc -l`
CRIT2=`$GREP  "H * z * \-0*\.0284" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2`
CTRL[6]=2

# First step
CRIT1=`$GREP "Li * 0*\.0000000000 * 0*\.0000000000 * (\-0|\-)\.12(50000|49999)" $log | wc -l`
CRIT2=`$GREP "H  * 0*\.0000000000 * 0*\.0000000000 * 3\.1(400000|399999)" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2`
CTRL[7]=2
ERROR[7]="FIRST STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-7\.894100" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.02475." $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2`
CTRL[8]=2
ERROR[8]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-7\.89781" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.01433." $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2`
CTRL[9]=2
ERROR[9]="THIRD ITERATION NOT CORRECT"

# Fourth iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-7\.89945" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.0096" $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2`
CTRL[10]=2
ERROR[10]="FOURTH ITERATION NOT CORRECT"

# Final iteration
CRIT1=`$GREP -l "Energy at this geometry is * \: * \-7\.902355" $log | wc -l`
CRIT2=`$GREP -l "Norm of gradient * \: * (0| )\.00000" $log | wc -l`
TEST[11]=`expr	$CRIT1 \+ $CRIT2`
CTRL[11]=2
ERROR[11]="FINAL ITERATION NOT CORRECT"

# Final gradient
CRIT1=`$GREP -l "Li * z * \-*0*\.00000" $log | wc -l`
CRIT2=`$GREP -l "H  * z * \-*0*\.00000" $log | wc -l`
TEST[12]=`expr	$CRIT1 \+ $CRIT2`
CTRL[12]=2
ERROR[12]="FINAL GRADIENT NOT CORRECT"

# Final Hessian
CRIT1=`$GREP "Li * z * 0*\.0022(4|5)." $log | wc -l`
CRIT2=`$GREP "H * z * \-0*\.0022(4|5). * 0*\.0022(4|5)." $log | wc -l`
CRIT3=`$GREP "Li     x * \-*0*\.00000." $log | wc -l`
CRIT4=`$GREP "H      x * \-*0*\.00000. * \-*0*\.00000." $log | wc -l`
CRIT5=`$GREP "Li     y * \-*0*\.00000." $log | wc -l`
CRIT6=`$GREP "H      y * \-*0*\.00000. * \-*0*\.00000." $log | wc -l`
CRIT9=`$GREP "Li * z * 0*\.000000 * 0*\.000000 * 0*\.0022(4|5)." $log | wc -l`
CRIT12=`$GREP "H * z * 0*\.000000 * 0*\.000000 * \-0*\.0022(4|5). * 0*\.000000 * 0*\.000000 * 0*\.0022(4|5)." $log | wc -l`
TEST[13]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT9 \+ $CRIT12`
CTRL[13]=11
ERROR[13]="FINAL HESSIAN NOT CORRECT"

# Frequency
CRIT1=`$GREP "1 * A1 * 259\... * (0| )\.00118. * 86[67]\..* * 20\.5" $log | wc -l`
TEST[14]=`expr	$CRIT1`
CTRL[14]=1
ERROR[14]="VIBRATIONAL FREQUENCY or IR INTENSITY NOT CORRECT"

# Partition function
CRIT1=`$GREP  "50\.00 * 1\.0289(D|E)\+04 * 13\... * 1\.0006 * 1\.3.....(D|E)\+05" $log | wc -l`
CRIT2=`$GREP "100\.00 * 5\.8202(D|E)\+04 * 25\... * 1\.024. * 1\.53....(D|E)\+06" $log | wc -l`
CRIT3=`$GREP "200\.00 * 3\.2924(D|E)\+05 * 51\... * 1\.182. * 1\.98....(D|E)\+07" $log | wc -l`
CRIT4=`$GREP "273\.15 * 7\.1769(D|E)\+05 * 69\... * 1\.341. * 6\.70....(D|E)\+07" $log | wc -l`
CRIT5=`$GREP "298\.15 * 8\.9335(D|E)\+05 * 75\... * 1\..... * 9\.49....(D|E)\+07" $log | wc -l`
CRIT6=`$GREP "300\.00 * 9\.0728(D|E)\+05 * 76\... * 1\.40.. * 9\.73....(D|E)\+07" $log | wc -l`
CRIT7=`$GREP "400\.00 * 1\.8625(D|E)\+06 * 101\... * 1\.64.. * 3\.12....(D|E)\+08" $log | wc -l`
CRIT8=`$GREP "500\.00 * 3\.2536(D|E)\+06 * 127\... * 1\..... * 7\.85....(D|E)\+08" $log | wc -l`
CRIT9=`$GREP "600\.00 * 5\.1323(D|E)\+06 * 152\... * 2\.15.. * 1\.68....(D|E)\+09" $log | wc -l`
CRIT10=`$GREP "700\.00 * 7\.5454(D|E)\+06 * 177\... * 2\.41.. * 3\.24....(D|E)\+09" $log | wc -l`
CRIT11=`$GREP "800\.00 * 1\.0536(D|E)\+07 * 203\... * 2\.6... * 5\.73....(D|E)\+09" $log | wc -l`
CRIT12=`$GREP "900\.00 * 1\.4143(D|E)\+07 * 228\... * 2\.94.. * 9\.51....(D|E)\+09" $log | wc -l`
CRIT13=`$GREP "1000\.00 * 1\.8405(D|E)\+07 * 253\... * 3\.20.. * 1\.49....(D|E)\+10" $log | wc -l`
CRIT14=`$GREP "1500\.00 * 5\.0718(D|E)\+07 * 380\... * 4\.53.. * 8\.75....(D|E)\+10" $log | wc -l`
CRIT15=`$GREP "2000\.00 * 1\.0411(D|E)\+08 * 507\... * 5\.8... * 3\.10....(D|E)\+11" $log | wc -l`
TEST[15]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14 \+ $CRIT15`
CTRL[15]=15
ERROR[15]="MOLECULAR PARTITION FUNCTION NOT CORRECT"

# Optimization
CRIT1=`$GREP "Geometry converged in * 12( |  )iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-7\.902356 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * (\-0|\-)\.013896 a\.u\." $log | wc -l`
TEST[16]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[16]=3
ERROR[16]="GEOMETRY OPTIMIZATION NOT CONVERGED CORRECTLY"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
# hjaaj aug 08: -ne test instead of -ne
# -> better when testing e.g. Hessian in geometry optimization.
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
